﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using WMSSoft.Lib.Basic;
using WMSSoft.Lib.Enums;
using WMSSoft.Lib.WinKStart;
using WMSSoft.Lib.Texte;
using WMSSoft.Lib.WinKStart.Model;
using WMSSoft.Lib.WinKStart.ViewModel;
using WMSSoft.Lib.WinKStart.Extension;
using WMSSoft.Lib.WinKStart.Abstract;
using WMSSoft.Lib.WinKStart.Model.MenuConfig;
using WMSSoft.Lib.WinKStart.Classes.Metadata;
using System.Reflection;

namespace WMSSoft.WinKStart.ViewModel
{
    public class Setting_AddActionSourceElementViewModel : CCommandViewModel
    {
        List<KeyValueAssignment> _SourceElements = null;
        List<KeyValueAssignment> _SourceElementProperties = null;
        ObservableCollection<KeyValueAssignment> _Test = new ObservableCollection<KeyValueAssignment>();
        SourceElement _Data = null;
        string _SelectedSourceElement = "";
        string _SelectedPropertyName = "";

        #region Initialize
        public Setting_AddActionSourceElementViewModel()
        {            
            try
            {
                base.ViewName = LanguageStrings.GetText("Add New Source-Element");

                _SourceElements = Meta_Elements.GetGUIElements("ALL");
                _Data = new SourceElement();
                PropertyChanged += Setting_AddActionSourceElementViewModel_PropertyChanged;

                m_oCommands.Add(new CSingleCommandViewModel(LanguageStrings.GetText("Close"), new CDelegatingCommand(Delegate => this.CMd_DiscardChanges())));
                m_oCommands.Add(new CSingleCommandViewModel(LanguageStrings.GetText("Ok"), new CDelegatingCommand(Delegate => this.CMd_OkDialog())));
            }
            catch (Exception Ex)
            {
                CMessagelogging.AppendMessage(LanguageStrings.GetText("Can't initialize ViewModel!"), Ex, EMessageTypes.Error);
            }
        }

        public Setting_AddActionSourceElementViewModel(SourceElement Data)
        {
            try
            {
                base.ViewName = LanguageStrings.GetText("Edit Source-Element");

                _SourceElements = Meta_Elements.GetGUIElements("ALL");
                PropertyChanged += Setting_AddActionSourceElementViewModel_PropertyChanged;

                _Data = Data;
                SelectedSourceElement = _Data.Element;
                SelectedPropertyName = _Data.Property;
                

                m_oCommands.Add(new CSingleCommandViewModel(LanguageStrings.GetText("Close"), new CDelegatingCommand(Delegate => this.CMd_DiscardChanges())));
                m_oCommands.Add(new CSingleCommandViewModel(LanguageStrings.GetText("Ok"), new CDelegatingCommand(Delegate => this.CMd_OkDialog())));
            }
            catch (Exception Ex)
            {
                CMessagelogging.AppendMessage(LanguageStrings.GetText("Can't initialize ViewModel!"), Ex, EMessageTypes.Error);
            }
        }

        #endregion

        #region Properties

        //public ObservableCollection<KeyValueAssignment> ListString
        //{
        //    get { return _Test; }
        //    set 
        //    {
        //        _Test = value;
        //        OnPropertyChanged("ListString");
        //    }
        //}

        public SourceElement Data
        {
            get { return _Data; }
        }

        public CSingleCommandViewModel Cmd_Ok
        {
            get { return m_oCommands[1]; }
        }

        public CSingleCommandViewModel Cmd_Close
        {
            get { return m_oCommands[0]; }
        }

        public List<KeyValueAssignment> SourceElements
        {
            get { return _SourceElements; }
        }

        public List<KeyValueAssignment> SourceElementProperties
        {
            get { return _SourceElementProperties; }
            set
            {
                _SourceElementProperties = value;
                OnPropertyChanged("SourceElementProperties");
            }
        }

        public string SelectedPropertyName
        {
            get { return _SelectedPropertyName; }
            set
            {
                _SelectedPropertyName = value;
                OnPropertyChanged("PropertyName");
            }
        }

        public string SelectedSourceElement
        {
            get { return _SelectedSourceElement; }
            set
            {
                _SelectedSourceElement = value;
                OnPropertyChanged("SelectedSourceElement");
            }
        }

        #endregion

        #region Command Procedures

        void CMd_DiscardChanges() 
        {
            try
            {
                Setting_AddActionSourceElement parentDlg = null;
                if ((Setting_AddActionSourceElement)((CDelegatingCommand)Cmd_Close.Command).CommandParameter != null)
                {
                    parentDlg = (Setting_AddActionSourceElement)((CDelegatingCommand)Cmd_Close.Command).CommandParameter;

                    if (parentDlg != null)
                        parentDlg.Close();
                }
            }
            catch (Exception Ex)
            {
                CMessagelogging.AppendMessage(LanguageStrings.GetText("Can't Close Dialog!"), Ex, EMessageTypes.Error);
            }
        }

        void CMd_OkDialog() 
        {
            try
            {
                Setting_AddActionSourceElement parentDlg = null;
                if ((Setting_AddActionSourceElement)((CDelegatingCommand)Cmd_Ok.Command).CommandParameter != null)
                {
                    parentDlg = (Setting_AddActionSourceElement)((CDelegatingCommand)Cmd_Ok.Command).CommandParameter;

                    if (parentDlg != null)
                    {
                        _Data.Element = SelectedSourceElement;
                        _Data.Property = SelectedPropertyName;
                        parentDlg.DialogResult = true;
                        parentDlg.Close();
                    }
                }
            }
            catch (Exception Ex)
            {
                CMessagelogging.AppendMessage(LanguageStrings.GetText("Can't apply Source-Element!"), Ex, EMessageTypes.Error);
            }
        }

        #endregion

        void Setting_AddActionSourceElementViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "SelectedSourceElement")
            {
                SourceElementProperties = Meta_Elements.GetGUIElementProperties(_SelectedSourceElement);
                SelectedPropertyName = "";
                ////ListString.Clear();
                //ListString = null;
                //ObservableCollection<KeyValueAssignment>  tmpListString = new ObservableCollection<KeyValueAssignment>();
                //foreach (KeyValueAssignment Item in SourceElementProperties)
                //{
                //    //ListString.Add(Item);
                //    tmpListString.Add(Item);
                //}

                //ListString = tmpListString;
            }
        }

    }
}
